﻿//-----------------------------------------------------------------------
// <copyright file="AgentConfig.cs" company="JiYou">
//     Copyright (c)
// </copyright>
// <author>Jill Ye</author>
//-----------------------------------------------------------------------
namespace PNunitToolsClass
{
    using System.IO;
    using System.Xml.Serialization;

    /// <summary>
    /// The pnunit agent config root attribute class
    /// </summary>
    [XmlRootAttribute(ElementName = "AgentConfig")]
    public class AgentConfig
    {
        /// <summary>
        /// The pnunit agent start port
        /// </summary>
        private int port;

        /// <summary>
        /// The pnunit assemblies path
        /// </summary>
        private string pathToAssemblies;

        /// <summary>
        /// Initializes a new instance of the <see cref="AgentConfig"/> class.
        /// </summary>
        public AgentConfig() 
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AgentConfig"/> class.
        /// </summary>
        /// <param name="port">The port, from 1~65535.</param>
        /// <param name="pathToAssemblies">The path to assemblies.</param>
        public AgentConfig(int port, string pathToAssemblies)
        {
            this.port = port;
            this.pathToAssemblies = pathToAssemblies;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AgentConfig"/> class.
        /// </summary>
        /// <param name="port">The port, from 1~65535.</param>
        public AgentConfig(int port)
        {
            this.port = port;
            this.pathToAssemblies = ".";
        }

        /// <summary>
        /// Gets or sets the port.
        /// </summary>
        /// <value>The port, from 1~65535.</value>
        [XmlElement(ElementName = "Port")]
        public int Port
        {
            get { return this.port; }
            set { this.port = value; }
        }

        /// <summary>
        /// Gets or sets the path to assemblies.
        /// </summary>
        /// <value>The path to assemblies.</value>
        [XmlElement(ElementName = "PathToAssemblies")]
        public string PathToAssemblies
        {
            get { return this.pathToAssemblies; }
            set { this.pathToAssemblies = value; }
        }

        /// <summary>
        /// Loads the agent file from file.
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <returns>Object Agentconfig</returns>
        public static AgentConfig Load(string filename)
        {
            Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read);
            AgentConfig agentConfig = Load(stream);
            stream.Close();
            return agentConfig;
        }

        /// <summary>
        /// Loads the agent file from file stream.
        /// </summary>
        /// <param name="fileStream">The file stream.</param>
        /// <returns>Object Agentconfig</returns>
        public static AgentConfig Load(Stream fileStream)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(AgentConfig));
            AgentConfig agentConfig = (AgentConfig)serializer.Deserialize(fileStream);
            return agentConfig;
        }

        /// <summary>
        /// Saves the agent config to file.
        /// </summary>
        /// <param name="filename">The filename.</param>
        public void Save(string filename)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(AgentConfig));
            FileStream stream = File.Create(filename);
            serializer.Serialize(stream, this);
            stream.Close();
        }   
    }
}